
package com.remenote.remenoteback.service.impl;


import com.github.benmanes.caffeine.cache.Cache;
import com.remenote.remenoteback.dao.UserRepository;
import com.remenote.remenoteback.entity.doo.UserDo;
import com.remenote.remenoteback.entity.resp.ServerResponse;
import com.remenote.remenoteback.entity.vo.UserVo;
import com.remenote.remenoteback.service.UserService;
import com.remenote.remenoteback.util.MD5Util;
import com.remenote.remenoteback.util.ThreadLocalUtil;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private Cache<String, UserVo> userCache;

    @Override
    public ServerResponse<UserVo> login(String username, String password) {
        String enablePassword = MD5Util.MD5EncodeUtf8(password);
        UserDo userDo = userRepository.findByUsernameAndPassword(username, enablePassword);
        if (userDo == null) {
            return ServerResponse.buildErrorResponse("username or password is invalid");
        }
        UserVo userVo = new UserVo().setToken("token_" + UUID.randomUUID().toString()).setUsername(username).setNickname(userDo.getNickname()).setGroupName(userDo.getGroupName());
        userCache.put(username, userVo);
        return ServerResponse.buildSuccessResponse(userVo);
    }

    @Override
    public ServerResponse logout() {
        userCache.invalidate(ThreadLocalUtil.getUserName());
        return ServerResponse.buildSuccessResponse();
    }

    @Override
    public ServerResponse validate(String username, String token) {
        UserVo userVo = userCache.getIfPresent(username);
        if (userVo == null || !userVo.getToken().equals(token)) {
            return ServerResponse.buildErrorResponse("token is invalid");
        } else {
            return ServerResponse.buildSuccessResponse();
        }
    }

    @Override
    public ServerResponse<UserVo> register(String username, String password) {
        UserDo userDo = userRepository.save(new UserDo().setUsername(username).setPassword(MD5Util.MD5EncodeUtf8(password)));
        UserVo userVo = new UserVo().setUsername(userDo.getUsername()).setNickname(userDo.getUsername());
        return ServerResponse.buildSuccessResponse(userVo);
    }

    @Transactional
    @Override
    public ServerResponse joinStudyGroup(String groupName) {
        UserDo userDo = userRepository.findByUsername(ThreadLocalUtil.getUserName());
        userDo.setGroupName(groupName);
        userDo = userRepository.save(userDo);
        return ServerResponse.buildSuccessResponse();
    }

    @Override
    public ServerResponse changeNickname(String newNickname) {
        UserDo userDo = userRepository.findByUsername(ThreadLocalUtil.getUserName());
        userDo.setNickname(newNickname);
        userRepository.save(userDo);
        return ServerResponse.buildSuccessResponse();
    }

    @Override
    public ServerResponse changePassword(String oldPassword, String newPassword) {
        UserDo userDo = userRepository.findByUsername(ThreadLocalUtil.getUserName());
        if (!userDo.getPassword().equals(MD5Util.MD5EncodeUtf8(oldPassword))) {
            return ServerResponse.buildErrorResponse("old password is invalid");
        }
        userDo.setPassword(MD5Util.MD5EncodeUtf8(newPassword));
        userRepository.save(userDo);
        return ServerResponse.buildSuccessResponse();
    }
}